﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Vit.Web.Services;
using Vit.Web.Helpers;

namespace Vit.Web.UI
{
    [ParseChildren()]
    public class ControlLoader : Control
    {
        public ControlLoader()
        {
            UrlReplaceWithUserProfile = false;
            UrlReplaceWithRouteData = false;
            LoadOnce = false;
            SupportDesign = false;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            string url = Url;

            url = URLHelper.SitePath(url, UrlReplaceWithUserProfile, UrlReplaceWithRouteData, false);

            if (LoadOnce && !InDesignMode)
            {
                if (this.Context.Items[url] != null)
                    return;
            }
            Control parent = this;

            if (InDesignMode)
            {
                if (SupportDesign)
                {
                    HtmlGenericControl designOverlay = new HtmlGenericControl("div");
                    designOverlay.ID = "designOverlay";
                    designOverlay.Attributes.Add("class", "designOverlay");
                    this.Controls.Add(designOverlay);

                    HtmlGenericControl designContainer = new HtmlGenericControl("div");
                    designContainer.ID = "designContainer";
                    designContainer.Attributes.Add("class", "designContainer");
                    this.Controls.Add(designContainer);
                    parent = designContainer;
                }
                else if (this.Parent is ContentReplace)
                {
                    HtmlGenericControl designControl = new HtmlGenericControl("div");
                    designControl.ID = "designControl";
                    designControl.Attributes.Add("class", "designControl");
                    this.Controls.Add(designControl);
                    parent = designControl;
                }
            }

            try
            {
                Control con = this.Page.LoadControl(url);
                List<string> contents = new List<string>();
                foreach (var cr in this.Controls.OfType<ContentReplace>())
                    contents.Add(cr.ID);
                foreach (string contentId in contents)
                {
                    ContentReplace content = this.FindControl(contentId) as ContentReplace;
                    if (content == null)
                        continue;
                    this.Controls.Remove(content);
                    Control holder = con.FindControl(content.ReplaceControlID);
                    if (holder != null)
                    {
                        holder.Controls.Clear();
                        holder.Controls.Add(content);
                    }
                }

                parent.Controls.Add(con);

                if (LoadOnce && !DesignMode)
                    this.Context.Items[url] = true;
            }
            catch
            {
                LiteralControl error = new LiteralControl();
                error.Text = string.Format("url: {0} is not found", url);
                parent.Controls.Add(error);
            }
        }

        public string Url { get; set; }
        public bool UrlReplaceWithUserProfile { get; set; }
        public bool UrlReplaceWithRouteData { get; set; }
        public bool LoadOnce { get; set; }

        public bool SupportDesign { get; set; }
        public bool InDesignMode
        {
            get
            {
                return this.Page.Request.Path.EndsWith(".design.aspx");
            }
        }
    }
}
